/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.tozo.service;

import hu.tozo.facade.AwsFacade;
import hu.tozo.facade.GlassfishFacade;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;

/**
 * Glassifsh szolgaltatasait meghivni kepes osztaly.
 * @author tozo
 */
//@Singleton
//@LocalBean
public class GlassfishService {
    GlassfishFacade gf;
    static String separator = System.getProperty("file.separator");
    static String homeDir = System.getProperty("user.home");
    
    
    public GlassfishService(){
        gf = new GlassfishFacade();
    }
    
    //Elindit egy glassfish node-ot
    //Pl n1, n2, n3, n4
    //Részletek a glassfish management feluleten
    public void startNode(String nodeName) throws UnsupportedEncodingException{
        gf.setNodeName(nodeName);
        gf.runTask("startNode");        
    }
    //Leallit egy glassfish node-ot
    public void stopNode(String nodeName) throws UnsupportedEncodingException{
        gf.setNodeName(nodeName);
        gf.runTask("stopNode");                
    }
    /*public void setNodeIpAddress(String ipAddress){
        gf.setIpAddress(ipAddress);
        gf.runTask("setNodeIp");
    }*/
    //Atallitja a dinamikus ip cimet az aktualisra
    //Bemenetkent meg kell adni a node nevét, és frissiti a metodus a DAS-ben
    public void refreshNodeIpAddress(String nodeName) throws Exception{
        //Betoltjuk a nodeokoat a property filebol.
        //A nodeName alapjan kikeressuk az Instance ID erteket.
        //Ez alapjan lekerjuk az aktualis ip cimet
        //Majd lefuttatjuk az ip beallito ant targetet, ami modositja a das-en az ip-t.
        Properties clusterNodes = new Properties();
        AwsFacade af = new AwsFacade();
        
        clusterNodes.load(loadAwsClusterNodesProperties());
        String nodeId = clusterNodes.getProperty(nodeName);
        String ipAddress = af.getPrivateIp(nodeId);
        gf.setIpAddress(ipAddress);
        gf.setNodeName(nodeName);
        gf.runTask("refreshNodeIp");
    }
    protected InputStream loadAwsClusterNodesProperties() throws FileNotFoundException{
        File f = new File(homeDir + separator + "aws" + separator + "AwsClusterNodes.properties");
        InputStream is = new FileInputStream(f);        
        return is;
    }
}
